home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / src / haeberli / libgutil / lut.c < prev    next >
C/C++ Source or Header  |  1994-08-01  |  3KB  |  100 lines

  1. /*
  2.  * Copyright 1991, 1992, 1993, 1994, Silicon Graphics, Inc.
  3.  * All Rights Reserved.
  4.  *
  5.  * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
  6.  * the contents of this file may not be disclosed to third parties, copied or
  7.  * duplicated in any form, in whole or in part, without the prior written
  8.  * permission of Silicon Graphics, Inc.
  9.  *
  10.  * RESTRICTED RIGHTS LEGEND:
  11.  * Use, duplication or disclosure by the Government is subject to restrictions
  12.  * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
  13.  * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
  14.  * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
  15.  * rights reserved under the Copyright Laws of the United States.
  16.  */
  17. /*
  18.  *    lut - 
  19.  *        Create and apply look up tables.
  20.  *
  21.  *                Paul Haebeli - 1990
  22.  */
  23. #include "lut.h"
  24.  
  25. float frand();
  26.  
  27. lut *makelut(func,insteps,outsteps,stoclut)
  28. float (*func)();
  29. int insteps, outsteps, stoclut;
  30. {
  31.     lut *l;
  32.     int i;
  33.     float low, high, inspace;
  34.  
  35.     l = (lut *)mymalloc(sizeof(lut));
  36.     l->insteps = insteps;
  37.     l->outsteps = outsteps;
  38.     l->stoclut = stoclut;
  39.     if(stoclut) {
  40.     l->flow = (float *)mymalloc(insteps*sizeof(float));
  41.     l->fhigh = (float *)mymalloc(insteps*sizeof(float));
  42.     l->fdelta = (float *)mymalloc(insteps*sizeof(float));
  43.     inspace = insteps-1.0;
  44.     for(i=0; i<insteps; i++) {
  45.         low = (i-0.499)/inspace;
  46.         high = (i+0.499)/inspace;
  47.         if(low<0.0) low = 0.0;
  48.         if(low>1.0) low = 1.0;
  49.         if(high>1.0) high = 1.0;
  50.         if(high<0.0) high = 0.0;
  51.         if((i==0) || (i == (insteps-1))) {
  52.         l->flow[i] = (func)(i/inspace);
  53.         l->fhigh[i] = (func)(i/inspace);
  54.         } else {
  55.         l->flow[i] = (func)(low);
  56.         l->fhigh[i] = (func)(high);
  57.         }
  58.         l->fdelta[i] = l->fhigh[i]-l->flow[i];
  59.     }
  60.     } else { 
  61.     l->stab = (unsigned short *)mymalloc(insteps*sizeof(unsigned short));
  62.     inspace = insteps-1.0;
  63.     for(i=0; i<insteps; i++) 
  64.         l->stab[i] = (l->outsteps-1)*(func)(i/inspace)+0.5;
  65.     }
  66.     return l;
  67. }
  68.  
  69. applylut(l,sptr,n)
  70. lut *l;
  71. unsigned short *sptr;
  72. int n;
  73. {
  74.     float val;
  75.     float *fdelta, *flow;
  76.     unsigned short *stab;
  77.     float outspace;
  78.     int ival;
  79.  
  80.     if(l->stoclut) {
  81.     fdelta = l->fdelta;
  82.     flow = l->flow;
  83.     outspace = l->outsteps-1;
  84.     while(n--) {
  85.         val = outspace*(flow[*sptr]+frand()*fdelta[*sptr]);
  86.         ival = val;
  87.         if((val-ival)<=frand()) 
  88.         *sptr++ = ival;
  89.         else
  90.         *sptr++ = ival+1;
  91.     }
  92.     } else {
  93.     stab = l->stab;
  94.     while(n--) {
  95.         *sptr = stab[*sptr];
  96.         sptr++;
  97.     }
  98.     }
  99. }
  100.